package com.ewzj.bigdata.app.utils

import android.app.Activity
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.support.v4.widget.SwipeRefreshLayout
import android.text.SpannableString
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.DelegatesExt
import com.ewzj.bigdata.mvp.ui.view.MyImageSpan
import com.ewzj.bigdata.mvp.ui.view.pathanimlib.PathAnimView
import com.ewzj.bigdata.mvp.ui.view.pathanimlib.res.StoreHousePath
import com.ewzj.bigdata.mvp.ui.view.pathanimlib.utils.PathParserUtils
import com.jess.arms.base.BaseApplication
import com.jess.arms.widget.CustomPopupWindow
import com.jess.arms.widget.imageloader.glide.GlideImageConfig
import okhttp3.RequestBody
import io.reactivex.android.schedulers.AndroidSchedulers





/**
 * author:zhangjing
 * 作用:
 * return:
 */
interface Apputils{
    object Utils{
        @JvmStatic
        fun json2Requestbody(json :String): RequestBody{
            val requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json)
            return requestBody
        }


        @JvmStatic
         fun setRefresh(swipeRefreshLayout: SwipeRefreshLayout) {
            swipeRefreshLayout.setColorSchemeResources(R.color.maingreen, R.color.color_blue)
        }
         @JvmStatic
        fun loadImgwithloading(context: Context,url: String,imgview: ImageView) {
             val application=context.applicationContext as BaseApplication
             val imageloader=application.appComponent.imageLoader()
             val imgconfig=GlideImageConfig.builder().errorPic(R.drawable.ic_loading)
                     .placeholder(R.drawable.ic_loading)
                     .url(url)
                     .imageView(imgview)
                     .build()
             imageloader.loadImage(context,imgconfig)

        }
        fun  getShareWindow(parentview: View):CustomPopupWindow{
            val context=parentview.context
            var contentview=LayoutInflater.from(context).inflate(R.layout.share_layout,null,false);
            var customPopupWindow=CustomPopupWindow.builder().contentView(contentview)
                    .animationStyle(R.style.mypopwindow_anim_style)
                    .isWrap(true)
                    .customListener(object :CustomPopupWindow.CustomPopupWindowListener{
                        override fun initPopupView(contentView: View?) {

                        }
                    })
                    .build()
            customPopupWindow.setOnDismissListener(object : PopupWindow.OnDismissListener{
                override fun onDismiss() {
                    setbackgroudAlpa(context,1f)
                }
            })
            customPopupWindow.width = LinearLayout.LayoutParams.MATCH_PARENT
            customPopupWindow.height = LinearLayout.LayoutParams.WRAP_CONTENT
//            customPopupWindow.showAtLocation(parentview, Gravity.BOTTOM, 0, 0)
            return customPopupWindow
        }

        fun setbackgroudAlpa(context: Context, alpa: Float) {
            AndroidSchedulers.mainThread().createWorker().schedule({
                val params = (context as Activity).getWindow().getAttributes()
                params.alpha = alpa
                (context as Activity).getWindow().setAttributes(params)
            })

        }


        /**
         * 产生shape类型的drawable

         * @param solidColor
         * *
         * @param strokeColor
         * *
         * @param strokeWidth
         * *
         * @param radius
         * *
         * @return
         */
        @JvmStatic
        fun getBackgroundDrawable(solidColor: Int, strokeColor: Int, strokeWidth: Int, radius: Float): GradientDrawable {
            val drawable = GradientDrawable()
            drawable.setColor(solidColor)
            drawable.setStroke(strokeWidth, strokeColor)
            drawable.cornerRadius = radius
            return drawable
        }
        @JvmStatic
        fun lefttopTextview(mTextView: TextView, string: String, resId: Int) {
            val msg = " " + " " + string
            val mImageSpan = MyImageSpan(mTextView.context, resId)
            val text = SpannableString(msg)
            text.setSpan(mImageSpan, 0, 1, 0)
            mTextView.text = text
        }

        fun getImgUrl(url: String): String {
            if (url.startsWith("http")) {
                return url
            } else {
                return AppKey.IMG_URL + url
            }
        }
        //    检查是否已经登录
        fun checkIsLogin(context: Context): Boolean {
            val token :String by DelegatesExt.preference(context,AppKey.API_TOKEN,"")
            return !token.isNullOrEmpty()
        }

        //    获取定位信息
        fun initLocation(mLocationClient: LocationClient) {
            val option = LocationClientOption()
            option.locationMode = LocationClientOption.LocationMode.Hight_Accuracy
            //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备

            option.setCoorType("bd09ll")
            //可选，默认gcj02，设置返回的定位结果坐标系

            val span = 1000
            option.setScanSpan(0)
            //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的

            option.setIsNeedAddress(true)
            //可选，设置是否需要地址信息，默认不需要

            option.isOpenGps = true
            //可选，默认false,设置是否使用gps

            option.isLocationNotify = false
            //可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果

            option.setIsNeedLocationDescribe(false)
            //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

            option.setIsNeedLocationPoiList(false)
            //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

            option.setIgnoreKillProcess(false)
            //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

            option.SetIgnoreCacheException(false)
            //可选，默认false，设置是否收集CRASH信息，默认收集

            option.setEnableSimulateGps(false)
            //可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

            mLocationClient.locOption = option
        }

        fun setPathandStart(rlloading: View) {
            rlloading.visibility = View.VISIBLE
            val pathAnimView1 = rlloading.findViewById(R.id.pathAnimView1) as PathAnimView
            pathAnimView1.visibility = View.VISIBLE
            val s = rlloading.resources.displayMetrics.density
            if (s <= 3) {
                pathAnimView1.sourcePath = PathParserUtils.getPathFromArrayFloatList(StoreHousePath.getPath("Mi Wo", s / 3f, (s / 3 * 16).toInt()))
            } else {
                pathAnimView1.sourcePath = PathParserUtils.getPathFromArrayFloatList(StoreHousePath.getPath("Mi Wo", 1f, 16))
            }

            //        pathAnimView1.getPathAnimHelper().isInfinite()
            pathAnimView1.startAnim()
        }
    }

}